#region AppServices License

// Product: 
// AppServices
// http://appservices.codeplex.com
//
// Author:
// Pawel Idzikowski
// http://www.polenter.com
// mailto:idzikowski@polenter.com?subject=AppServices-Question
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the author(s) be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//    1. The origin of this software must not be misrepresented; you must not
//      claim that you wrote the original software. If you use this software
//      in a product, an acknowledgment in the product documentation would be
//      appreciated but is not required.
//
//    2. Altered source versions must be plainly marked as such, and must not
//      be misrepresented as being the original software.
//
//    3. This notice may not be removed or altered from any source distribution.

#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Polenter.Services.Internal
{
    /// <summary>
    /// Estimates all types from which the type is dependent. These are types of its properties and fields marked with ServiceAttribute.
    /// </summary>
    internal class InjectedTypeProvider
    {
        /// <summary>
        /// Every thread has its own static cache with injected types.
        /// In .NET Compact there is only one cache for all threads!
        /// </summary>
#if !Smartphone
        [ThreadStatic] 
#endif
        private static InjectedTypeInfoCollection _cache;

        private readonly InjectedMemberProvider _injectedMemberProvider = new InjectedMemberProvider();

        public InjectedTypeInfo GetInfo(Type type)
        {
            InjectedTypeInfoCollection cache = getCache();
            if (!cache.Contains(type))
            {
                InjectedTypeInfo info = createInjecedTypeInfo(type);
                cache.Add(info);
            }
            return cache[type];
        }

        private static InjectedTypeInfoCollection getCache()
        {
            if (_cache==null)
            {
                _cache=new InjectedTypeInfoCollection();
            }
            return _cache;
        }

        private InjectedTypeInfo createInjecedTypeInfo(Type type)
        {
            InjectedMemberInfo injectedMemberInfo = _injectedMemberProvider.GetInjectedMembers(type);

            var injectedTypes = new List<Type>();

            forEach(injectedMemberInfo.Fields,
                fieldInfo =>
                {
                    if (!injectedTypes.Contains(fieldInfo.FieldType))
                        injectedTypes.Add(fieldInfo.FieldType);
                });

            forEach(injectedMemberInfo.Properties,
                propertyInfo =>
                              {
                                  if (!injectedTypes.Contains(propertyInfo.PropertyType))
                                      injectedTypes.Add(propertyInfo.PropertyType);
                              });

            var info = new InjectedTypeInfo(type, injectedTypes.ToArray());
            return info;
        }

        private static void forEach<T>(T[] items, Action<T> callback)
        {
            foreach (var item in items)
            {
                callback(item);
            }
        }
    }

    internal class InjectedTypeInfo
    {
        public InjectedTypeInfo(Type type, Type[] injectedTypes)
        {
            Type = type;
            InjectedTypes = injectedTypes;
        }

        /// <summary>
        /// Type of object which contains properties and fields to be injected
        /// </summary>
        public Type Type { get; set; }

        /// <summary>
        /// Types of properties and fields to be injected (marked with ServiceAttribute)
        /// </summary>
        public Type[] InjectedTypes { get; set; }
    }

    internal class InjectedTypeInfoCollection : KeyedCollection<Type, InjectedTypeInfo>
    {
        protected override Type GetKeyForItem(InjectedTypeInfo item)
        {
            return item.Type;
        }
    }
}